﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.Net.Sockets;
using ProgNetComponentsDx.Chat;

namespace WindowsFormsApplication1
{
    public partial class Form2 : XtraForm
    {
        private static Dictionary<string, Form2> allForms = new Dictionary<string, Form2>();
        
        public static void AddForm(Form2 form)
        {
            allForms.Add(form.nick, form);
        }
        public static Form2 GetForm(string nick)
        {
            if (allForms.ContainsKey(nick))
                return allForms[nick];
            return null;
        }

        public Form2()
        {
            InitializeComponent();
        }
        
        ChatClient client;
        string nick;
        ChatMessageInfo info = new ChatMessageInfo();

        public Form2(ChatClient client, string nick)
        {
            InitializeComponent();
            this.client = client;
            this.nick = nick;
            info.User = nick;
            info.Type = ChatMessages.StringMsg;
            client.MessageReceived += new ChatMessageHandler(client_MessageReceived);
        }

        void client_MessageReceived(object sender, ChatMessageEventArgs e)
        {
            if (e.Info.User == nick)
            {
                if (e.Info.Type == ChatMessages.StringMsg)
                {
                    this.memoEdit1.AppendLine(nick + ": " + e.Info.Message);
                }
                else if (e.Info.Type == ChatMessages.UserDisconnected)
                {
                    this.memoEdit1.AppendLine("** User " + nick + " has disconnected **");
                }
            }
        } 

        private void btnWyslij_Click(object sender, EventArgs e)
        {
            if (meText.Text.IsNotNull())
            {
                info.Message = meText.Text;
                this.memoEdit1.AppendLine("ja: " + meText.Text);
                client.SendMessage(info);
            }
        }
        
    }
}
